package com.gip.controller;

import com.gip.pojo.User;
import com.gip.service.UserService;
import com.mysql.cj.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Queue;

@Controller
public class LoginController {
    @Autowired
    UserService userService;

    @GetMapping("/login")
    public String login() {
        return "login";
    }

    @PostMapping("/toLogin")
    public String toLogin(User user, HttpSession session, Model model) {
        User realUser = userService.findUserById(user.getId());
        if(realUser==null){
            model.addAttribute("msg", "用户不存在!");
            return "/index";
        }

        if(!user.getPassword().equals(realUser.getPassword())){
            model.addAttribute("msg", "账号密码不匹配!");
            return "/index";
        }

            if (realUser.getRole() != user.getRole()) {
                model.addAttribute("msg", "账号类型错误!");
                return "/index";
            }
            if(realUser.getState()==0){
                model.addAttribute("msg", "账号被禁用!");
                return "/index";
            }

        session.setAttribute("User",realUser);
        System.out.println(user);
        return "redirect:/goMain";
    }

    @RequestMapping("/goMain")
    public String goMain() {
        return "main";
    }

    @RequestMapping("/layout")
    public String layout(HttpSession session) {
        session.invalidate();
        return "index";
    }
    @RequestMapping(value={"/","/index"})
    public String index(){
        return "index";
    }


}
